<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true">
      <el-form-item label="分类">
        <category-selector :category-path.sync="categoryPath"></category-selector>
      </el-form-item>
      <el-form-item label="品牌">
        <brand-select :category-id="categoryId" :brand-id.sync="brandId" style="width:160px"></brand-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select style="width:160px" v-model="queryParams.status" clearable>
          <el-option label="新建" :value="0"></el-option>
          <el-option label="上架" :value="1"></el-option>
          <el-option label="下架" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="loadData">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="selections.length === 0"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
    </el-row>

    <el-table
      :data="dataList"
      border
      v-loading="loading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="name" header-align="center" align="center" label="名称">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.name" placement="top">
            <span style="width: 200px;overflow: hidden;white-space: nowrap">{{ scope.row.name }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="center" label="主图片">
        <template slot-scope="scope">
          <image-preview :src="scope.row.mainImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column prop="categoryPathName" header-align="center" width="200" align="center"
                       label="分类"></el-table-column>
      <el-table-column prop="sort" header-align="center" width="200" align="center" label="排序"></el-table-column>
      <el-table-column prop="brandName" header-align="center" align="center" label="品牌"></el-table-column>
      <el-table-column prop="publishStatus" header-align="center" align="center" label="上架状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0">新建</el-tag>
          <el-tag v-if="scope.row.status === 1">已上架</el-tag>
          <el-tag v-if="scope.row.status === 2">已下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" width="200"
                       label="创建时间"></el-table-column>
      <el-table-column prop="publishStatus" header-align="center" align="center" label="管理SKU">
        <template slot-scope="scope">
          <el-button size="mini" icon="el-icon-setting" @click="showSkus(scope.row)" type="primary" circle></el-button>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="200" label="操作">
        <template slot-scope="scope">
          <el-button icon="el-icon-sell" v-if="scope.row.status === 0 || scope.row.status === 2" type="text"
                     size="small" @click="productUp(scope.row.id)">上架
          </el-button>
          <el-button icon="el-icon-download" v-if="scope.row.status === 1" type="text" size="small"
                     @click="productDown(scope.row.id)">下架
          </el-button>
          <el-button icon="el-icon-edit" v-if="scope.row.status === 0 || scope.row.status === 2" type="text"
                     size="small" @click="attrUpdateShow(scope.row)">修改
          </el-button>
          <el-button icon="el-icon-delete" v-if="scope.row.status === 0 || scope.row.status === 2" type="text"
                     size="small" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <sku-list ref="skuList"></sku-list>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="loadData"
    />
  </div>
</template>

<script>
import {deleteSpu, findSpu, spuDown, spuUp} from '@/api/product/spu'
import BrandSelect from "@/views/common/brandSelect.vue";
import CategorySelector from "@/views/common/categorySelector.vue";
import SkuList from "@/views/product/spu/SkuList.vue";

export default {
  components: {SkuList, CategorySelector, BrandSelect},
  data() {
    return {
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        brandId: '',
        categoryId: ''
      },
      categoryPath: [],
      brandId: '',
      categoryId: '',
      total: 0,
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      loading: false,
      selections: []
    };
  },
  props: {
    catId: {
      type: Number,
      default: 0
    }
  },
  created() {
    this.loadData();
  },
  watch: {
    categoryPath(val) {
      if (val.length === 3) {
        this.queryParams.categoryId = val[2]
        this.categoryId = val[2]
        return
      }
      this.categoryId = ''
    },
    brandId(val) {
      this.queryParams.brandId = val
    }
  },
  methods: {
    productUp(id) {
      spuUp(id).then(res => {
        if (res.success) {
          this.$message.success('商品上架成功')
          this.loadData()
        }
      });
    },
    productDown(id) {
      spuDown(id).then(res => {
        if (res.success) {
          this.$message.success('商品下架成功')
          this.loadData()
        }
      })
    },
    showSkus(row) {
      this.$refs.skuList.open(row.id)
    },
    attrUpdateShow(row) {
      this.$router.push({
        path: "/commodity/edit",
        query: {
          spuId: row.id
        }
      });
    },
    resetQuery() {
      this.queryParams = {
        pageNo: 1,
        pageSize: 10,
        brandId: '',
        categoryId: ''
      }
      this.categoryPath = []
      this.brandId = ''
      this.loadData()
    },
    loadData() {
      this.loading = true;
      findSpu(this.queryParams).then((res) => {
        if (res.success) {
          this.dataList = res.data.records
          this.total = res.data.total;
        }
        this.loading = false;
      });
    },
    // 多选
    selectionChangeHandle(val) {
      this.selections = val;
    },
    handleAdd() {
      this.$router.push({path: "/commodity/edit"})
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除此商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteSpu(row.id).then(res => {
          if (res.success) {
            this.$message.success('删除成功');
            this.loadData()
          }
        })
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    }
  },
};
</script>
